[% setvar title docs/pdds/pdd11_extending.pod - The Parrot Extension System %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='NAME'></a><h1>NAME</h1>
<p>docs/pdds/pdd11_extending.pod - The Parrot Extension System</p>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>The extension API for Parrot is a simple, somewhat abstract,
interface to Parrot for code written in C or other compiled
languages. It provides about the same level of access to Parrot
that bytecode programs have.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>The API presents to C programs roughly the same interface presented to
bytecode programs--that is, a C extension can see everything that a
bytecode program can see, including Parrot's base architecture,
registers, stacks, and whatnot. This view isn't required, however, and
often extension code won't need or want to know what Parrot's internal
structures look like. For this reason the functions in the extension
API are divided into two broad groups, one that has no particular
knowledge of the internals and one that does.</p>
<p>The stability of the two API groups is guaranteed separately. Group 1,
the internals unaware group, should be good basically forever. Group
2, the internals aware group, is only guaranteed for the lifetime of
the current architecture. (It's likely that both groups will last
equally long; however, the Group 1 interface could reasonably be
emulated on a different engine, while the Group 2 interface is more
closely tied to Parrot).</p>
<p><b>Note:</b> The extension API has not yet been completely specified. New
functions may be added, and those described below may change or be
removed. You have been warned...</p>
<a name='API - Group 1: Internals-unaware functions'></a><h1>API - Group 1: Internals-unaware functions</h1>
<p>These functions are the ones that are largely unaware of the structure
and architecture of Parrot. They deal mainly in data as abstract
entities, and Parrot itself as a black box that, at best, can make
subroutine or method calls. This is sufficient for many extensions
which act as black box processing units and in turn treat Parrot
itself as a black box.</p>
<a name='PMC access functions'></a><h2>PMC access functions</h2>
<p>The following functions are for storing and retrieving data inside PMCs.
Note that use of the _keyed functions with non-aggregate PMCs will
generally just result in Parrot throwing an exception.</p>
<ul>
<li><a name='Parrot_PMC_get_string(interp, pmc)'></a><code>Parrot_PMC_get_string(interp, pmc)</code></li>
<p>Returns a Parrot_STRING that represents the string value of the PMC.</p>
<li><a name='Parrot_PMC_get_string_intkey(interp, pmc, Parrot_Int key)'></a><code>Parrot_PMC_get_string_intkey(interp, pmc, Parrot_Int key)</code></li>
<p>Keyed version of <code>Parrot_PMC_get_string</code>. Returns a Parrot_STRING
representing the string value of whatever is stored at the element of the
PMC indexed by <code>key</code>.</p>
<li><a name='Parrot_PMC_get_pointer(interp, pmc)'></a><code>Parrot_PMC_get_pointer(interp, pmc)</code></li>
<p>Returns a pointer to some item of data. The details of what the pointer
points to depend on the particular PMC. This function is useful for
dealing with PMCs that hold pointers to arbitrary data.</p>
<li><a name='Parrot_PMC_get_pointer_intkey(interp, pmc, Parrot_Int key)'></a><code>Parrot_PMC_get_pointer_intkey(interp, pmc, Parrot_Int key)</code></li>
<p>A keyed version of <code>Parrot_PMC_get_pointer</code>. Returns the pointer value
of whatever is stored at the element of the PMC indexed by <code>key</code>.</p>
<li><a name='Parrot_PMC_get_intval(interp, pmc)'></a><code>Parrot_PMC_get_intval(interp, pmc)</code></li>
<p>Returns the integer value of the PMC.</p>
<li><a name='Parrot_PMC_get_intval_intkey(interp, pmc, Parrot_Int key)'></a><code>Parrot_PMC_get_intval_intkey(interp, pmc, Parrot_Int key)</code></li>
<p>A keyed version of <code>Parrot_PMC_get_intval</code>. Returns the integer value
of whatever is stored at the element of the PMC indexed by <code>key</code>.</p>
<li><a name='Parrot_PMC_get_numval(interp, pmc)'></a><code>Parrot_PMC_get_numval(interp, pmc)</code></li>
<p>Returns the numeric value of the PMC.</p>
<li><a name='Parrot_PMC_get_numval_intkey(interp, pmc, Parrot_Int key)'></a><code>Parrot_PMC_get_numval_intkey(interp, pmc, Parrot_Int key)</code></li>
<p>A keyed version of <code>Parrot_PMC_get_numval</code>. Returns the numeric value
of whatever is stored at the element of the PMC indexed by <code>key</code>.</p>
<li><a name='Parrot_PMC_get_cstring(interp, pmc)'></a><code>Parrot_PMC_get_cstring(interp, pmc)</code></li>
<p>Returns a C-string (char *) that represents the string value of the
PMC. The memory the char * points to is a copy of the original value,
and changing it will not change the original in any way.</p>
<p>This memory will <i>not</i> be reclaimed by garbage collection, nor may it
be returned to the system with <code>free</code>. It must be returned with
<code>Parrot_free_cstring</code>.</p>
<li><a name='Parrot_PMC_get_cstring_intkey(interp, pmc, Parrot_Int key)'></a><code>Parrot_PMC_get_cstring_intkey(interp, pmc, Parrot_Int key)</code></li>
<p>A keyed version of <code>Parrot_PMC_get_cstring</code>. Returns a C-string
representing the string value of whatever is stored at the element of
the PMC indexed by <code>key</code>.</p>
<li><a name='Parrot_PMC_get_cstringn(interp, pmc, &amp;len)'></a><code>Parrot_PMC_get_cstringn(interp, pmc, &amp;len)</code></li>
<p>Returns a C-string (char *) that represents the string value of the
PMC. The memory the char * points to is a copy of the original value,
and changing it will not change the original in any way. The <code>len</code>
parameter is the address of an integer that will get the length of the
string as Parrot knows it.</p>
<p>This memory will <i>not</i> be reclaimed by garbage collection, nor may it
be returned to the system with <code>free</code>. It must be returned with
<code>Parrot_free_cstring</code>.</p>
<li><a name='Parrot_PMC_get_cstringn_intkey(interp, pmc, &amp;len, Parrot_Int key)'></a><code>Parrot_PMC_get_cstringn_intkey(interp, pmc, &amp;len, Parrot_Int key)</code></li>
<p>A keyed version of <code>Parrot_PMC_get_cstringn</code>. Returns a C-string
representing the string value of whatever is stored at the element of
the PMC indexed by <code>key</code>. Stores the length of the string in <code>len</code>.</p>
<li><a name='Parrot_PMC_get_pmc_intkey(interp, pmc, Parrot_Int key)'></a><code>Parrot_PMC_get_pmc_intkey(interp, pmc, Parrot_Int key)</code></li>
<p>Returns the PMC stored at the element of the passed-in PMC that is
indexed by <code>key</code>.</p>
<li><a name='Parrot_PMC_set_string(interp, pmc, Parrot_STRING value)'></a><code>Parrot_PMC_set_string(interp, pmc, Parrot_STRING value)</code></li>
<p>Assign the passed-in Parrot_STRING to the passed-in PMC.</p>
<li><a name='Parrot_PMC_set_string_intkey(interp, pmc, Parrot_STRING value, Parrot_Int key)'></a><code>Parrot_PMC_set_string_intkey(interp,
pmc, Parrot_STRING value, Parrot_Int key)</code></li>
<p>Keyed version of <code>Parrot_PMC_set_string</code>. Assigns <code>value</code> to the PMC stored
at element &lt;key&gt; of the passed-in PMC.</p>
<li><a name='Parrot_PMC_set_pointer(interp, pmc, void *value)'></a><code>Parrot_PMC_set_pointer(interp, pmc, void *value)</code></li>
<p>Assign the passed-in pointer to the passed-in PMC.</p>
<li><a name='Parrot_PMC_set_pointer_intkey(interp, pmc, void *value, Parrot_Int key)'></a><code>Parrot_PMC_set_pointer_intkey(interp, pmc, void *value, Parrot_Int key)</code></li>
<p>Keyed version of <code>Parrot_PMC_set_pointer</code>. Assigns <code>value</code> to the PMC stored
at element &lt;key&gt; of the passed-in PMC.</p>
<li><a name='Parrot_PMC_set_pmc_intkey(interp, pmc, Parrot_PMC value, Parrot_Int key)'></a><code>Parrot_PMC_set_pmc_intkey(interp, pmc, Parrot_PMC value, Parrot_Int key)</code></li>
<p>Assigns <code>value</code> to the PMC stored at element &lt;key&gt; of the passed-in PMC.</p>
<li><a name='Parrot_PMC_set_intval(interp, pmc, Parrot_Int value)'></a><code>Parrot_PMC_set_intval(interp, pmc, Parrot_Int value)</code></li>
<p>Assign the passed-in Parrot integer to the passed-in PMC.</p>
<li><a name='Parrot_PMC_set_intval_intkey(interp, pmc, Parrot_Int value, Parrot_Int key)'></a><code>Parrot_PMC_set_intval_intkey(interp,
pmc, Parrot_Int value, Parrot_Int key)</code></li>
<p>Keyed version of <code>Parrot_PMC_set_intval</code>. Assigns <code>value</code> to the PMC stored
at element &lt;key&gt; of the passed-in PMC.</p>
<li><a name='Parrot_PMC_set_numval(interp, pmc, Parrot_Float value)'></a><code>Parrot_PMC_set_numval(interp, pmc, Parrot_Float value)</code></li>
<p>Assign the passed-in Parrot number to the passed-in PMC.</p>
<li><a name='Parrot_PMC_set_numval_intkey(interp, pmc, Parrot_Float value, Parrot_Int key)'></a><code>Parrot_PMC_set_numval_intkey(interp,
pmc, Parrot_Float value, Parrot_Int key)</code></li>
<p>Keyed version of <code>Parrot_PMC_set_numval</code>. Assigns <code>value</code> to the PMC stored
at element &lt;key&gt; of the passed-in PMC.</p>
<li><a name='Parrot_PMC_set_cstring(interp, pmc, const char *value)'></a><code>Parrot_PMC_set_cstring(interp, pmc, const char *value)</code></li>
<p>Convert the passed-in null-terminated C string to a Parrot_STRING and
assign it to the passed-in PMC.</p>
<li><a name='Parrot_PMC_set_cstring_intkey(interp, pmc, const char *value, Parrot_Int key)'></a><code>Parrot_PMC_set_cstring_intkey(interp,
pmc, const char *value, Parrot_Int key)</code></li>
<p>Keyed version of <code>Parrot_PMC_set_cstring</code>. Converts <code>value</code> to a
Parrot_STRING and assigns it to the PMC stored at element &lt;key&gt; of the
passed-in PMC.</p>
<li><a name='Parrot_PMC_set_cstringn(interp, pmc, const char *value, Parrot_Int length)'></a><code>Parrot_PMC_set_cstringn(interp,
pmc, const char *value, Parrot_Int length)</code></li>
<p>Convert the passed-in null-terminated C string to a Parrot_STRING of length
<code>length</code> and assign it to the passed-in PMC. If <code>value</code> is longer than
<code>length</code>, then only the first <code>length</code> characters will be converted. If
<code>value</code> is shorter than <code>length</code>, then the extra characters in the
Parrot_STRING should be assumed to contain garbage.</p>
<li><a name='Parrot_PMC_set_cstringn_intkey(interp, pmc, const char *value, Parrot_int length, Parrot_Int key)'></a><code>Parrot_PMC_set_cstringn_intkey(interp,
pmc, const char *value, Parrot_int length, Parrot_Int key)</code></li>
<p>Keyed version of <code>Parrot_PMC_set_cstringn</code>. Converts the first <code>length</code>
characters of <code>value</code> to a Parrot_STRING and assigns the resulting string
to the PMC stored at element &lt;key&gt; of the passed-in PMC.</p>
</ul>
<a name='Creation and destruction'></a><h2>Creation and destruction</h2>
<p>Functions used to create and destroy PMCs, Parrot_STRINGs, etc.</p>
<ul>
<li><a name='Parrot_PMC_new(interp, Parrot_Int typenum)'></a><code>Parrot_PMC_new(interp, Parrot_Int typenum)</code></li>
<p>Creates and returns a new PMC. <code>typenum</code> is an integer identifier that
specifies the type of PMC required. The <code>typenum</code> corresponding to a
particular PMC class name can be found using <code>Parrot_PMC_typenum</code>.</p>
<li><a name='Parrot_PMC_typenum(interp, const char* class)'></a><code>Parrot_PMC_typenum(interp, const char* class)</code></li>
<p>Returns the internal integer identifier corresponding to a PMC with
class name <code>class</code>.</p>
<li><a name='Parrot_PMC_null()'></a><code>Parrot_PMC_null()</code></li>
<p>Returns the special <code>NULL</code> PMC.</p>
<li><a name='Parrot_new_string(interp, char *buffer, int length, Parrot_Encoding encoding, Parrot_CharType charset, Parrot_Language language, Parrot_Int flags)'></a><code>Parrot_new_string(interp,
char *buffer, int length, Parrot_Encoding encoding,
Parrot_CharType charset, Parrot_Language language, Parrot_Int flags)</code></li>
<p>Create a new Parrot string from a passed-in buffer. If the <code>encoding</code>,
<code>charset</code>, or <code>language</code> are unspecified (i.e. if you pass in 0), then
the defaults are used. Otherwise, the functions <code>Parrot_find_encoding</code>,
<code> Parrot_find_chartype</code> and <code>Parrot_find_language</code> (all described below)
can be used to find the appropriate values for a particular choice of
encoding, chartype or language.</p>
<p>Flag values are currently undocumented.</p>
<p>Note that a copy of the buffer is made.</p>
<li><a name='Parrot_find_encoding(interp, char *encoding_name)'></a><code>Parrot_find_encoding(interp, char *encoding_name)</code></li>
<p>Find the magic token for an encoding, by name.</p>
<li><a name='Parrot_find_chartype(interp, char *chartype)'></a><code>Parrot_find_chartype(interp, char *chartype)</code></li>
<p>Find the magic token for a chartype, by name.</p>
<li><a name='Parrot_find_language(interp, char *language)'></a><code>Parrot_find_language(interp, char *language)</code></li>
<p>Find the magic token for a language, by language name.</p>
<li><a name='Parrot_free_cstring(char* string)'></a><code>Parrot_free_cstring(char* string)</code></li>
<p>Deallocates a C string that the interpreter has handed to you.
This function must be used to free strings produced by
<code>Parrot_PMC_get_cstring</code> and <code>Parrot_PMC_get_cstringn</code>, as these
will not be reclaimed by the garbage collector. It should not be used
to deallocate strings that do not come from Parrot.</p>
<li><a name='Parrot_register_pmc(interp, pmc)'></a><code>Parrot_register_pmc(interp, pmc)</code></li>
<p>Add a reference to the PMC to the interpreter's DOD registry. This
prevents PMCs known only to extensions from getting destroyed during
DOD runs.</p>
<li><a name='Parrot_unregister_pmc(interp, pmc)'></a><code>Parrot_unregister_pmc(interp, pmc)</code></li>
<p>Remove a reference to the PMC from the interpreter's DOD registry.
If the reference count reaches zero, the PMC will be destroyed during the
next DOD run.</p>
</ul>
<a name='Subroutine and method calls'></a><h2>Subroutine and method calls</h2>
<p>Functions to call Parrot subroutines and methods</p>
<ul>
<li><a name='Parrot_call(interp, Parrot_PMC sub, Parrot_Int argcount, ...)'></a><code>Parrot_call(interp, Parrot_PMC sub, Parrot_Int argcount, ...)</code></li>
<p>Calls a Parrot subroutine, with <code>argcount</code> PMC parameters. This function
sets up Parrot's registers in line with the Parrot calling conventions;
see <i><a href='http://search.cpan.org/perldoc?pdd03_calling_conventions.pod'>pdd03_calling_conventions.pod</a></i> for more details.</p>
<li><a name='Parrot_call_method(interp, Parrot_PMC sub, Parrot_STRING method, Parrot_Int argcount, ...)'></a><code>Parrot_call_method(interp,
Parrot_PMC sub, Parrot_STRING method, Parrot_Int argcount, ...)</code></li>
<p>Calls a Parrot method named <code>method</code> with <code>argcount</code> PMC parameters.
NB. This is not yet implemented and may change.</p>
</ul>
<a name='API - Group 2: Internals aware'></a><h1>API - Group 2: Internals aware</h1>
<p>The internals-aware functions are for those extensions that need to
query or alter the state of Parrot's internals in some way.</p>
<p>Register access functions</p>
<p>The following functions allow the values stored in Parrot's registers
to be accessed. An attempt to access a non-existent register (e.g.
string register -123) will cause the function to throw an exception
(well, it will once we actually implement some bounds-checking...).
The value stored in an uninitialized register is undefined; it may well
be zero (or NULL), but do not rely on this being the case.</p>
<ul>
<li><a name='Parrot_get_intreg(interp, Parrot_Int regnum)'></a><code>Parrot_get_intreg(interp, Parrot_Int regnum)</code></li>
<p>Return the value of an integer register.</p>
<li><a name='Parrot_get_numreg(interp, Parrot_Int regnum)'></a><code>Parrot_get_numreg(interp, Parrot_Int regnum)</code></li>
<p>Return the value of a numeric register.</p>
<li><a name='Parrot_get_strreg(interp, Parrot_Int regnum)'></a><code>Parrot_get_strreg(interp, Parrot_Int regnum)</code></li>
<p>Return the value of a string register.</p>
<li><a name='Parrot_get_pmcreg(interp, Parrot_Int regnum)'></a><code>Parrot_get_pmcreg(interp, Parrot_Int regnum)</code></li>
<p>Return the value of a PMC register.</p>
</ul>
<a name='ATTACHMENTS'></a><h1>ATTACHMENTS</h1>
<p>None.</p>
<a name='FOOTNOTES'></a><h1>FOOTNOTES</h1>
<p>None.</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p><b><i>docs/glossary.pod</i></b></p>
<a name='VERSION'></a><h1>VERSION</h1>
<a name='CURRENT'></a><h2>CURRENT</h2>
<pre>    Maintainer: 
    Class: Internals
    PDD Number: 11
    Version: 1.0
    Status: Developing
    Last Modified: February 20, 2004 
    PDD Format: 1
    Language: English</pre>
<a name='HISTORY'></a><h2>HISTORY</h2>
<ul>
<li><a name='Version 1'></a>Version 1</li>
<p>None. First version</p>
</ul>
<a name='CHANGES'></a><h1>CHANGES</h1>
<ul>
<li><a name='Version 1.0'></a>Version 1.0</li>
<p>None. First version</p>
</ul>
</div>
